package com.vm.activity;

import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.StringRequest;
import com.vm.commons.CurrentUser;
import com.vm.commons.VMConst;
import com.vm.db.LocalUserInfoDao;
import com.vm.model.LocalUserInfo;
import com.vm.utils.MyVolley;
import com.wwtscapp.apk.R;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class ChangePsdActivity extends Activity {
	protected static final String TAG = null;
	EditText et_old_pwd;
	EditText et_new_pwd;
	EditText et_confirm_pwd;
	Button btn_register;
	ImageView imgbtn_search_back;
	private RequestQueue mQueue;
	private LocalUserInfoDao luidao = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.change_password);
		luidao = new LocalUserInfoDao(ChangePsdActivity.this);
		mQueue = MyVolley.getRequestQueue();
		init();
	}

	public void init() {
		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		imgbtn_search_back.setOnClickListener(btn_backListener);
		et_old_pwd = (EditText) findViewById(R.id.et_old_pwd);
		et_new_pwd = (EditText) findViewById(R.id.et_new_pwd);
		et_confirm_pwd = (EditText) findViewById(R.id.et_confirm_pwd);
		btn_register = (Button) findViewById(R.id.btn_register);
		btn_register.setOnClickListener(btn_listener);
	}

	OnClickListener btn_listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String old_pwd = et_old_pwd.getText().toString();
			final String new_pwd = et_new_pwd.getText().toString();
			String confirm_pwd = et_confirm_pwd.getText().toString();
			if (old_pwd.equals("") || new_pwd.equals("")
					|| confirm_pwd.equals("")) {
				Toast toast = Toast.makeText(ChangePsdActivity.this, "输入不能为空",
						Toast.LENGTH_SHORT);
				toast.show();
			} else if (!old_pwd.equals(CurrentUser.USERPSD)) {
				Toast toast = Toast.makeText(ChangePsdActivity.this, "原密码不正确",
						Toast.LENGTH_SHORT);
				toast.show();
			} else if (!new_pwd.equals(confirm_pwd)) {
				Toast toast = Toast.makeText(ChangePsdActivity.this,
						"两次输入的密码不一致", Toast.LENGTH_SHORT);
				toast.show();
			} else {
				String url = VMConst.REQUEST_URL + "changePwd?id="
						+ CurrentUser.USERID + "&password=" + new_pwd;
				try {
					StringRequest stringRequest = new StringRequest(url,
							new Listener<String>() {

								@Override
								public void onResponse(String arg0) {
									// TODO Auto-generated method stub
									Toast toast = Toast.makeText(
											ChangePsdActivity.this, "修改密码成功",
											Toast.LENGTH_SHORT);
									toast.show();
									CurrentUser.USERPSD = new_pwd;
									LocalUserInfo localUserInfo = new LocalUserInfo();
									localUserInfo.setPsd(new_pwd);
									localUserInfo.setUserId(CurrentUser.USERID);
									luidao.updatePwd(localUserInfo);
									onBackPressed();
								}

							}, null);
					mQueue.add(stringRequest);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					Log.e(TAG, e.getMessage());
				}
			}
		}

	};
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// Log.i(TAG, "imgbtn_search_back返回按钮监听");
			onBackPressed();

		}
	};

	public void onBackPressed() {
		super.onBackPressed();
		System.out.println("按下了back键   onBackPressed()");
	}
}